<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="变更ID" prop="changeId">
        <el-input
          v-model="queryParams.changeId"
          placeholder="请输入变更ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产任务ID" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入生产任务ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产任务号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入生产任务号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图纸名称" prop="drawingName">
        <el-input
          v-model="queryParams.drawingName"
          placeholder="请输入图纸名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图纸描述" prop="drawingRemark">
        <el-input
          v-model="queryParams.drawingRemark"
          placeholder="请输入图纸描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变更类型" prop="changeType">
        <el-select v-model="queryParams.changeType" placeholder="请选择变更类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="逻辑删除标识" prop="flag">
        <el-input
          v-model="queryParams.flag"
          placeholder="请输入逻辑删除标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段1" prop="attr1">
        <el-input
          v-model="queryParams.attr1"
          placeholder="请输入预留字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段2" prop="attr2">
        <el-input
          v-model="queryParams.attr2"
          placeholder="请输入预留字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段3" prop="attr3">
        <el-input
          v-model="queryParams.attr3"
          placeholder="请输入预留字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <FileUpload
            :limit="100"
            :fileSize="500"
            :fileType="fileType"
            :value="pictureList"
            :isShowTip="isShowTip"
            :uploadData="uploadData"
            @onUploaded="handleUploaded"
            v-hasPermi="['md:changeLine:add']"
            v-if="optType === 'add'"
        ></FileUpload>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['md:changeLine:edit']"
          v-if="optType === 'add'"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:changeLine:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:changeLine:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-loading="loading"
        :data="changeLineList"
        border
        @cell-mouse-enter="handleCellEnter()"
        @cell-mouse-leave="handleCellLeave()"
    >
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="图纸名称" align="center" prop="drawingName" />
      <el-table-column label="图号" align="center" prop="drawingNumber" />
      <el-table-column label="图纸描述" align="center" prop="drawingRemark" >
        <div class="item" slot-scope="scope">
          <el-input
              class="item_input"
              v-model="scope.row.drawingRemark"
              size="mini"
              type="textarea"
              style="width: 100%;"
              :title="scope.row.drawingRemark"
              @blur="handleBlur(scope.row)"
              v-if="optType === 'add'"
          />
          <el-input
              class="item_input"
              v-model="scope.row.drawingRemark"
              size="mini"
              type="textarea"
              style="width: 100%;"
              :title="scope.row.drawingRemark"
              readonly
              v-if="optType === 'view'"
          />
        </div>
      </el-table-column>
      <el-table-column label="变更类型" align="center" prop="changeType" />
      <el-table-column label="操作时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="optType !== 'view'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleReturn(scope.row)"
            v-hasPermi="['md:changeLine:edit']"
            v-if="scope.row.changeType === '删除'"
          >移除变更</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:changeLine:remove']"
            v-if="scope.row.changeType === '新增'"
          >移除变更</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图纸变更line对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table v-loading="deleteLoading" :data="deleteList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="图纸名称" align="center" prop="drawingName" />
        <el-table-column label="图号" align="center" prop="drawingNumber" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                v-hasPermi="['md:changeLine:query']"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="1300px" append-to-body>
      <iframe
          :src="viewUrl"
          frameborder="0"
          scrolling="auto"
          style="width: 100%; height: 600px;"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChangeLine,
  getChangeLine,
  delChangeLine,
  addChangeLine,
  updateChangeLine,
  deleteList,
  deleteBatch,
  returnEdit
} from "@/api/mes/md/changeLine";
import FileUpload from "./fileUpload.vue";


export default {
  name: "ChangeLine",
  components: { FileUpload },
  props: {
    taskId: null,
    changeId: null,
    optType: null,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸变更line表格数据
      changeLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeId: this.changeId,
        taskId: this.taskId,
        taskCode: null,
        drawingName: null,
        drawingNumber: null,
        drawingUrl: null,
        drawingRemark: null,
        changeType: null,
        flag: null,
        attr1: null,
        attr2: null,
        attr3: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        changeId: [
          { required: true, message: "变更ID不能为空", trigger: "blur" }
        ],
        taskId: [
          { required: true, message: "生产任务ID不能为空", trigger: "blur" }
        ],
        flag: [
          { required: true, message: "逻辑删除标识不能为空", trigger: "blur" }
        ],
      },
      // 上传类型
      fileType: ["pdf"],
      // 图纸列表
      pictureList: [],
      // 上传提示开关
      isShowTip: false,
      // 上传附带参数
      uploadData: { taskId : this.taskId, changeId: this.changeId },
      // 可修改单元格属性
      editProp: ['drawingRemark'],
      // 删除列表总数
      deleteTotal: 0,
      // 遮罩层
      deleteLoading: true,
      // 删除列表
      deleteList: [],
      // 查询参数
      deleteQuery: {
        pageNum: 1,
        pageSize: 10,
        changeId: null,
        taskId: null,
        taskCode: null,
        drawingName: null,
        drawingNumber: null,
        drawingUrl: null,
        drawingRemark: null,
        changeType: null,
        flag: null,
        attr1: null,
        attr2: null,
        attr3: null,
      },
      // 查看开关
      viewOpen: false,
      // 查看URL
      viewUrl: null,
      // 标题
      viewTitle: '图纸查看',
    };
  },
  created() {
    this.getList();
  },
  watch: {
    taskId: function (newVal, oldVal) {
      this.queryParams.taskId = newVal;
      if (newVal !== null) {
        this.uploadData.taskId = newVal;
      }
      this.getList();
    },
    changeId: function (newVal, oldVal) {
      this.queryParams.changeId = newVal;
      if (newVal !== null) {
        this.uploadData.changeId = newVal;
      }
      this.getList();
    }
  },
  methods: {
    /** 查询图纸变更line列表 */
    getList() {
      this.loading = true;
      listChangeLine(this.queryParams).then(response => {
        this.changeLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        changeId: null,
        taskId: this.taskId,
        taskCode: null,
        drawingName: null,
        drawingNumber: null,
        drawingUrl: null,
        drawingRemark: null,
        changeType: null,
        flag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item =>  ({...item, changeId: this.changeId}))
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图纸变更line";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.open = true;
      this.deleteLoading = true;
      this.title = "删除图纸";
      this.getDeleteList();
    },
    /** 任务原图纸 **/
    getDeleteList() {
      this.deleteQuery.taskId = this.queryParams.taskId;
      deleteList(this.deleteQuery).then(response => {
        this.deleteList = response.rows;
        this.deleteTotal = response.total;
        this.deleteLoading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      const ids = this.ids;
      deleteBatch(ids).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.open = false;
        this.getList();
      })
      /*this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.flag = 0;
            updateChangeLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChangeLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });*/
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认移除“' + row.drawingName + '"的图纸变更？').then(function() {
        return delChangeLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("移除成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleReturn(row) {
      returnEdit(row).then(response => {
        this.$modal.msgSuccess("移除成功");
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/changeLine/export', {
        ...this.queryParams
      }, `changeLine_${new Date().getTime()}.xlsx`)
    },
    // 上传成功
    handleUploaded(data) {
      this.$modal.msgSuccess("上传成功")
      this.getList();
    },
    // 列表单元格鼠标进入事件
    handleCellEnter (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'block'
        }
      }
    },
    // 列表单元格鼠标离开事件
    handleCellLeave (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'none'
        }
      }
    },
    // 列表单元格输入框失去焦点事件
    handleBlur(row) {
      updateChangeLine(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    // 查看图纸
    handleView(row) {
      this.viewOpen = true;
      this.viewUrl = row.drawingUrl;
    },
  }
};
</script>
